﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmRoleDetail
    /// Version:1.0
    /// Date:2022-11-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为角色设置
    /// </remarks>
    public partial class frmRoleDetail : DevComponents.DotNetBar.OfficeForm
    {
        public int m_nOperateKind = 0;//0:查看 1:新增 2:修改 
        public Boolean m_bIsRefresh = false;    //是否需刷新
        public SystemRole m_systemRole = new SystemRole();

        public frmRoleDetail()
        {
            InitializeComponent();
        }

        #region 01 自定义方法
        /// <summary>
        /// 数据初始化
        /// </summary>
        private void _init()
        {
            txtCode.Text = m_systemRole.m_strCode;
            txtName.Text = m_systemRole.m_strName;
            txtCode.Focus();
            txtCode.ReadOnly = false;
            txtName.ReadOnly = false;
            cbIsNew.Visible = m_systemRole.m_nOperate == 1;
            //01 权限初始化
            _initLimit();
        }

        /// <summary>
        /// 初始化权限
        /// </summary>
        public void _initLimit()
        {
            try
            {
                //显示权限
                DataView dv = AppInfo.m_dtFunction.DefaultView;
                dv.Sort = "ParentCode,ShowOrder";
                tvLimit.Nodes.Clear();
                CommFun._setTreeView0(dv, tvLimit, "", m_systemRole.m_strLimit, true);
            }
            catch (Exception ex)
            {
                CommFun._messageBoxShow(ex.ToString());
            }
        }
       
        /// <summary>
        /// 保存扫码信息
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="bIsNew"></param>
        private Boolean _saveCode()
        {
            Boolean bResult = false;
            m_systemRole.m_strCode = txtCode.Text.Trim();
            m_systemRole.m_strName = txtName.Text.Trim();
            string strLimit = "";
            string strLimitInfo = "";
            CommFun._getTreeView(tvLimit, ref strLimit, ref strLimitInfo);// 取权限值
            m_systemRole.m_strLimit = strLimit;

            if (m_systemRole.m_strCode.Length < 1)
            {
                CommFun._messageBoxShow("编号不能为空!");
                return false;
            }
            if (m_systemRole.m_strName.Length < 1)
            {
                CommFun._messageBoxShow("名称不能为空!");
                return false;
            }
            
            // 保存数据
            if (!m_systemRole._checkName())
            {
                return false;
            }

            if (m_systemRole._saveInfo() < 1)
            {
                CommFun._messageBoxShow("数据保存失败");
            }
            else
            {
                m_bIsRefresh = true;
                CommFun._messageBoxShow("数据保存成功");
                if (cbIsNew.Checked)    // 继续新增
                {
                    m_systemRole._init();
                    _init();
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }                
            }
            return bResult;
        }
        
        #endregion

        #region  02 窗体相关事件方法
        private void frmConfig_Load(object sender, EventArgs e)
        {
            // 初始化
            _init();
            cbIsNew.Checked = m_systemRole.m_nOperate == 1;
        }

        private void tvLimit_NodeDoubleClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            // 双击全选
            DevComponents.AdvTree.Node nodeCurrent = e.Node;
            nodeCurrent.Checked = !nodeCurrent.Checked;
            foreach (DevComponents.AdvTree.Node node in nodeCurrent.Nodes)
            {
                node.Checked = nodeCurrent.Checked;
            }
        }       

        private void btnSave_Click(object sender, EventArgs e)
        {
            _saveCode();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
